﻿using DevExpress.XtraEditors.Controls;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RecruitStudents.Client.Examinee
{
    public partial class Examinee_ScoreSearchForm_Export_List : BaseForm
    {
        BLL.ServiceComm serviceComm = new BLL.ServiceComm();
        BLL.ServiceExaminee serviceExam = new BLL.ServiceExaminee();
        public Examinee_ScoreSearchForm_Export_List()
        {
            InitializeComponent();
            InitData();
        }

        #region 初始页面
        private void InitData()
        {
            #region 绑定考点列表
            DataTable dtExamCity = serviceComm.GetExamPlaceTable();
            DataRow dr = dtExamCity.NewRow();
            dr["ExamPlaceNO"] = "";
            dr["ExamPlaceName"] = "";
            dtExamCity.Rows.InsertAt(dr, 0);

            txtExamCity.Properties.DataSource = dtExamCity;
            txtExamCity.Properties.Columns.Clear();
            txtExamCity.Properties.Columns.Add(new LookUpColumnInfo("ExamPlaceNO", 30, "考点代码"));
            txtExamCity.Properties.Columns.Add(new LookUpColumnInfo("ExamPlaceName", 55, "考点名称"));
            txtExamCity.Properties.ValueMember = "ExamPlaceNO";
            txtExamCity.Properties.DisplayMember = "ExamPlaceName";
            txtExamCity.Properties.NullText = "";
            txtExamCity.EditValue = string.IsNullOrEmpty(SessionerHelper.ExamPlaceID) ? "310000" : SessionerHelper.ExamPlaceID;
            #endregion

            #region 绑定专业
            DataTable dtSpecialityKinds = serviceComm.GetSpecialityKindsTable(SessionerHelper.SystemYear);
            DataRow drpecialityKinds = dtSpecialityKinds.NewRow();
            drpecialityKinds["SpecialityKindsNO"] = "";
            drpecialityKinds["SpecialityKindsName"] = "";
            dtSpecialityKinds.Rows.InsertAt(drpecialityKinds, 0);
            comboSpecialityKindsNO.Properties.DataSource = dtSpecialityKinds;
            comboSpecialityKindsNO.Properties.Columns.Clear();
            comboSpecialityKindsNO.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsNO", 30, "专业代码"));
            comboSpecialityKindsNO.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsName", 55, "专业名称"));
            comboSpecialityKindsNO.Properties.ValueMember = "SpecialityKindsNO";
            comboSpecialityKindsNO.Properties.DisplayMember = "SpecialityKindsName";
            comboSpecialityKindsNO.Properties.NullText = "";
            comboSpecialityKindsNO.Properties.Columns[0].Visible = false;
            #endregion

            BindAppointmentGroup("", "", ""); //绑定考试分组
        }
        #endregion

        #region 绑定考试分组
        private void BindAppointmentGroup(string queryExamCity, string querySpecialityKindsNO, string queryExamLevel)
        {
            comboExamRoom.Properties.DataSource = null;
            comboExamRoom.Properties.NullText = "";
            comboExamRoom.EditValue = null;
            string ExamPlaceID = string.Empty;
            string ExamPlaceName = string.Empty;
            if (txtExamCity.EditValue != null)
            {
                ExamPlaceID = txtExamCity.EditValue.ToSafeString();
            }
            string SpecialityKindsNO = string.Empty;
            if (comboSpecialityKindsNO.EditValue != null)
            {
                SpecialityKindsNO = comboSpecialityKindsNO.EditValue.ToSafeString();
            }
            string ExamLevel = "1";
            if (ExamPlaceID.IsNullOrEmptyString() || SpecialityKindsNO.IsNullOrEmptyString() || ExamLevel.IsNullOrEmptyString())
            {
                return;
            }
            using (DataTable dt = serviceComm.GetExamRoomByPlaceID_Speciality_Level_CS(ExamPlaceID, SpecialityKindsNO, ExamLevel))
            {
                comboExamRoom.Properties.DataSource = dt;
                comboExamRoom.Properties.Columns.Clear();
                comboExamRoom.Properties.Columns.Add(new LookUpColumnInfo("ExamRoomNO", 30, "考场编号"));
                comboExamRoom.Properties.Columns.Add(new LookUpColumnInfo("ExamRoomName", 55, "考场名称"));
                comboExamRoom.Properties.ValueMember = "ExamRoomNO";
                comboExamRoom.Properties.DisplayMember = "ExamRoomName";
                comboExamRoom.Properties.NullText = "";
            }
        }
        #endregion

        #region 下拉框选择事件
        private void txtExamCity_EditValueChanged(object sender, EventArgs e)
        {
            BindAppointmentGroup("", "", "");
        }

        private void comboSpecialityKindsNO_EditValueChanged(object sender, EventArgs e)
        {
            BindAppointmentGroup("", "", "");
        }

        private void comboExamLevel_EditValueChanged(object sender, EventArgs e)
        {
            BindAppointmentGroup("", "", "");
        }
        #endregion


        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            if (comboExamRoom.EditValue == null || comboExamRoom.EditValue.ToSafeString() == "")
            {
                DevHelper.ShowErrorMessageBox("请先选择考场进行导出!");
                return;
            }
            if (comboSpecialityKindsNO.EditValue == null || comboSpecialityKindsNO.EditValue.ToSafeString() == "")
            {
                DevHelper.ShowErrorMessageBox("请先选择专业进行导出!");
                return;
            }
            DataSet ds = serviceExam.GetExamineeScoreSearchFormExcel(txtExamCity.EditValue.ToSafeString(), comboSpecialityKindsNO.EditValue.ToSafeString(),
              "1", txtName.Text, txtIdCard.Text, txtNMT_SignUpID.Text, comboExamRoom.EditValue.ToSafeString(), SessionerHelper.SystemYear, txtStartDate.Text, txtEndDate.Text);
            if (ds.Tables.Count == 0)
            {
                //string str = "      评委：肖英          考场： 红楼102          打印时间:2015/12/20 11:26:31";
                DevHelper.ShowErrorMessageBox("您选择的考场没有进行考生考试!");
                return;
            }
            try
            {
                string strTemplateFile = Application.StartupPath + "\\Template\\统考打分情况.xls";
                SaveFileDialog kk = new SaveFileDialog();
                kk.Title = "保存EXECL文件";
                kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";
                kk.FilterIndex = 1;
                kk.FileName = comboExamRoom.Text + "考官打分表.xls";
                kk.ShowDialog();
                if (kk.FileName.IndexOf(":") < 0) return; //被点了取消 

                foreach (DataTable dt in ds.Tables)
                {
                    string strExaminerName = dt.TableName.Replace("1", "").Replace("2", "").Replace("3", "").Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "");
                    if (comboSpecialityKindsNO.EditValue.ToSafeString() == "1303090_B")
                    {
                        DataTable dtNew_WGLD = dt.Copy();
                        dtNew_WGLD.Columns.Remove("Score2");

                        string[] strFile = kk.FileName.Split('.');
                        string saveFileName = strFile[0] + "_" + strExaminerName + "_文稿朗读_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "" + "." + strFile[1];
                        string strTitle = string.Empty;
                        strTitle = SessionerHelper.SystemYear + "年上海市普通高校播音与主持艺术专业统考 文稿朗读 科目考试成绩校对表（按100分制）";
                        string strTitle2 = " 评委：" + strExaminerName + "     考场： " + comboExamRoom.Text + "       打印时间:" + DateTime.Now.DateTimeToyyyy_MM_dd_HH_mm_ss();
                        NPOIHelper.ExportExcelForDtByTemplate_New(dtNew_WGLD, strTemplateFile, saveFileName, strTitle, strTitle2);

                        DataTable dtNew_CYZS = dt.Copy();
                        dtNew_CYZS.Columns.Remove("Score1");

                        saveFileName = strFile[0] + "_" + strExaminerName + "_才艺展示_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "" + "." + strFile[1];
                        strTitle = SessionerHelper.SystemYear + "年上海市普通高校播音与主持艺术专业统考 才艺展示 科目考试成绩校对表（按100分制）";
                        strTitle2 = " 评委：" + strExaminerName + "     考场： " + comboExamRoom.Text + "       打印时间:" + DateTime.Now.DateTimeToyyyy_MM_dd_HH_mm_ss();
                        NPOIHelper.ExportExcelForDtByTemplate_New(dtNew_CYZS, strTemplateFile, saveFileName, strTitle, strTitle2);
                    }
                    else
                    {
                        DataTable dtNew = dt;
                        dtNew.Columns.Remove("Score2");
                        string[] strFile = kk.FileName.Split('.');
                        string saveFileName = strFile[0] + "_" + strExaminerName + "" + DateTime.Now.ToString("yyyyMMddHHmmss") + "" + "." + strFile[1];
                        string strTitle = string.Empty;
                        if (comboSpecialityKindsNO.EditValue.ToSafeString().Contains("1303010"))
                        {
                            strTitle = SessionerHelper.SystemYear + "年上海市普通高校表演类专业统考 " + comboSpecialityKindsNO.Text + " 科目考试成绩校对表（按100分制）";
                        }
                        else
                        {
                            strTitle = SessionerHelper.SystemYear + "年上海市普通高校播音与主持艺术专业统考 " + comboSpecialityKindsNO.Text + " 科目考试成绩校对表（按100分制）";
                        }
                        string strTitle2 = " 评委：" + strExaminerName + "     考场： " + comboExamRoom.Text + "       打印时间:" + DateTime.Now.DateTimeToyyyy_MM_dd_HH_mm_ss();
                        NPOIHelper.ExportExcelForDtByTemplate_New(dtNew, strTemplateFile, saveFileName, strTitle, strTitle2);
                    }
                }
                DevHelper.ShowInfoMessageBox("导出考生成绩成功！");
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("导出成绩异常：" + ex.Message);
            }
        }
    }
}
